package smu.KYBank.test;

import smu.KYBank.DBHandler;
import smu.KYBank.KYBmain;
import android.database.Cursor;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;

public class KYBmainTest2 extends ActivityInstrumentationTestCase2<KYBmain> {
	private KYBmain mActivity;
	Button deposit, withdraw;
	String login_id = "hello";
	String acnt, bal, r_type;
	
	public KYBmainTest2() {
		super("smu.KYBank", KYBmain.class);
	}
		
	protected void setUp() throws Exception {
		super.setUp();
		
		mActivity = this.getActivity();
		deposit = (Button) mActivity.findViewById(smu.KYBank.R.id.deposit);
		withdraw = (Button) mActivity.findViewById(smu.KYBank.R.id.withdraw);

		try {
			runTestOnUiThread(new Runnable() {
				
				public void run() {
					DBHandler dbhandler = DBHandler.open(mActivity);
					Cursor cursor = dbhandler.select_id(login_id + "	");
//					startManagingCursor(cursor);
					if (cursor.moveToFirst()) {
						do {
							acnt = cursor.getString(cursor.getColumnIndex("account_num"));
							// bal = cursor.getString(cursor.getColumnIndex("balance"));
						} while (cursor.moveToNext());
					}
					Cursor r_check = dbhandler.r_select(login_id + "	");
//					startManagingCursor(r_check);
					if (r_check.moveToFirst()) {
						do {
							r_type = r_check.getString(r_check.getColumnIndex("type"));
						} while (r_check.moveToNext());
					} else
						r_type = "no request";
					dbhandler.close();
					
				}
			});
		} catch (Throwable e) {
			// TODO: handle exception
		}
	}

	// case: login_id "hello" has no account -> deposit is not allowed -> return false.
	public void testBtnDepositClicked() {
		assertFalse(mActivity.btnDepositClicked(acnt));
	}

	// case: login_id "hello" has no account -> withdraw is not allowed -> return false.
	public void testBtnWithdrawClicked() {
		assertFalse(mActivity.btnWithdrawClicked(acnt));
	}

	// case: login_id "hello" has no account -> create request -> return 1.
	public void testBtnCreateClicked() {
		assertEquals(1, mActivity.btnCreateClicked(r_type, acnt));
	}
	
	// case: login_id "hello" has no account -> Inquire is not allowed -> return false.
	public void testBtnInquireClicked() {
		assertFalse(mActivity.btnInquireClicked(acnt));
	}

	// case: login_id "hello" has no account -> delete request -> return 1.
	public void testBtnDeleteClicked() {
		assertEquals(1, mActivity.btnDeleteClicked(r_type, acnt));
	}

}
